﻿using BySoft.Server.Business;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class UpTechFilePage : BasePage
    {
        static string strSessionName = "SessionPdf";

        private DataTable CreateUpFileDT()
        {
            return MdProduct.CreateUpFileDT();
        }

        public DataTable DtTechFile
        {
            set { Session[strSessionName] = value; }
            get
            {
                if (Session[strSessionName] == null)
                {
                    return CreateUpFileDT();
                }
                else
                {
                    return (DataTable)Session[strSessionName];
                }
            }
        } 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                string id =UIHelper.GetQueryStringValue("id");
                hidFileFormType.Value = id;

                strSessionName = MdProduct.QuerySessionFileName(id);
              

            }
        }

        private bool SaveFile(string strDir, string strFormType, FileUpload fi)
        {
            string strName = DateTime.Now.ToString("yyyyMMddHHmmss");
            string strServerDir = Server.MapPath(strDir);
            if (!Directory.Exists(strServerDir))
            {
                Directory.CreateDirectory(strServerDir);
            }
            //string strFileName = strServerDir + strName;
            if (fi.HasFile)
            {
                string strUpFileName = fi.FileName.Trim();
                string strExtension = strUpFileName.Substring(strUpFileName.LastIndexOf("."));
                if (!string.IsNullOrEmpty(strExtension))
                {
                    string strSaveFileName = strName + strExtension;
                    string strSavePath = strServerDir + strSaveFileName;
                    fi.SaveAs(strSavePath);
                    FileInfo finfo = new FileInfo(strSavePath);
                    DataTable dtTemp = DtTechFile.Copy();
                    DataRow drNew = dtTemp.NewRow();
                    drNew["AttachmentID"] = Guid.NewGuid().ToString();
                    drNew["FileType"] = finfo.Extension;
                    drNew["FileSize"] = Convert.ToInt32(finfo.Length);
                    drNew["FileURL"] = finfo.Name;
                    drNew["FileName"] = strUpFileName;
                    drNew["FormType"] = strFormType;
                    drNew["UpDateTime"] = DateTime.Now;
                    string path = "/UpFile/";
                    path += strFormType + "/" + drNew["FileURL"];
                    path = Request.Url.Scheme + "://" + Request.Url.Authority + path;
                    drNew["OpenURL"] = path;

                    dtTemp.Rows.Add(drNew);
                    DtTechFile = dtTemp.Copy();
                    return true;
                }
            }
            return false;
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            string strPath = "";
            string strFormType = "";
            switch (hidFileFormType.Value.Trim())
            {
                case "1": strFormType = "BSBOM"; strPath = "~\\UpFile\\BSBOM\\"; break;
                case "2": strFormType = "BSBOMHalf"; strPath = "~\\UpFile\\BSBOMHalf\\"; break;
                case "3": strFormType = "Material"; strPath = "~\\UpFile\\Material\\"; break;
                case "4": strFormType = "entity"; strPath = "~\\UpFile\\entity\\"; break;
                case "5": strFormType = "ProductMap"; strPath = "~\\UpFile\\ProductMap\\"; break;
                case "6": strFormType = "ProductTech"; strPath = "~\\UpFile\\ProductTech\\"; break;
                case "7": strFormType = "KnifeMap"; strPath = "~\\UpFile\\KnifeMap\\"; break;
                default :strFormType = "entity"; strPath = "~\\UpFile\\"; break;
            }

            if (SaveFile(strPath, strFormType, fimport))
            {
                string sReturn = "";
                sReturn = "success";
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'" + sReturn + "',1);CloseTopDialog();</script>");
            }
        }
    }
}